package cetcbigdata.da.service.guangdong.provincial_general_office;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Gunagdong extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        this.parseListPage(recordSender, fieldsList);
    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage(){
        try{
            String listUrl = "https://www.gdzwfw.gov.cn/portal/custom-config/gdbsItemCommon/commonIndex";
            JSONObject headers = new JSONObject();
            headers.put("Referer", "https://apps.gdzwfw.gov.cn/");
            headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36");
            headers.put("Set-Cookie", "portal=3CEE257C8FDF300BC2D1DFA37B307971; Path=/portal; HttpOnly");
            JSONObject parmas = new JSONObject();
            String listResponse =  postFormReq(listUrl, headers, parmas);
            JSONObject responseObj = JSONObject.parseObject(listResponse);
            if(responseObj.getString("statusCode").equals("200")){
                JSONArray listTemp = responseObj.getJSONObject("data").getJSONArray("data");
                JSONObject content = new JSONObject();
                List<List<JSONObject>> data = new ArrayList<>();
                for (Object o : listTemp){
                    JSONArray list = (JSONArray) o;

                    //获取事项分类
                    List<JSONObject> sort = new ArrayList<>();
                    for (Object i : list){
                        JSONObject item = (JSONObject) i;
                        JSONObject itemName = new JSONObject();
                        itemName.put("名称", item.getString("name") + " " +
                                (isNotBlankOrNull(item.getString("desc")) ? item.getString("desc") : ""));

                        //社会保障分类
                        JSONArray items = item.getJSONArray("items");
                        List<JSONObject> scoalList = new ArrayList<>();
                        if (items != null && !items.isEmpty()){
                            for (Object e : items){
                                JSONObject itemsList = (JSONObject) e;
                                JSONObject scoalData = new JSONObject();
                                scoalData.put("名称", itemsList.getString("name"));

                                //分类
                                JSONArray itemOrg = itemsList.getJSONArray("items");
                                List<JSONObject> scoalOrgList = new ArrayList<>();
                                if (itemOrg != null && !itemOrg.isEmpty()){
                                    for (Object a : itemOrg){
                                        JSONObject itemsList1 = (JSONObject) a;
                                        JSONObject itemTitle = new JSONObject();
                                        itemTitle.put("名称", itemsList1.getString("name"));
                                        scoalOrgList.add(itemTitle);
                                    }
                                }

                                scoalData.put("分类", scoalOrgList);
                                scoalList.add(scoalData);
                            }
                            itemName.put("分类", scoalList);
                        }

                        sort.add(itemName);
                    }
                    data.add(sort);
                }
                content.put("全省通办清单", data);
                return processResult(listUrl, listResponse, content);
            }
            return new JSONObject();
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JSONObject();
    }

    private void parseListPage(RecordSender recordSender, List<Object> fieldsList){
    }

    // 结果封装
    private JSONObject processResult(String listUrl, String listPageHtml, JSONObject content){
        JSONObject result = new JSONObject();
        result.put("region", "广东省");
        result.put("module", "全省通办清单");
        result.put("listUrl", listUrl);
        result.put("list_page_html", listPageHtml);
        result.put("content", content.toJSONString());

        return result;
    }

    public static void main(String[] args) {
        Gunagdong spider = new Gunagdong();
        System.out.println(spider.getDataDemo());
    }
}
